package com.wangyg.juc.ProducerConsumer.version1;

import java.util.Random;
import java.util.concurrent.BlockingQueue;

/**
 * Created by wangyg6 on 2018/7/5.
 */
public class Consumer implements Runnable {
	private BlockingQueue<String> queue;
	private static final int SLEEPTIME = 1000;

	public Consumer(BlockingQueue<String> queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		System.out.println("start Consumer id :" + Thread.currentThread().getId());
		Random r = new Random();
		try {
			while (true) {
				String data = queue.take();
				if (data != null) {
					System.out.println(Thread.currentThread().getName()+"消费了"+data);
					Thread.sleep(/*r.nextInt(SLEEPTIME)*/10);
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			Thread.currentThread().interrupt();
		}
	}
}
